package no.uio.chat;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;

public class Client extends Thread{
	Socket s;
	ConnectionManager cm;
	DataOutputStream os = null;
	String me = "";
	Checker ch;
	Client(Socket s, ConnectionManager cm){
		this.s = s;
		this.cm = cm;
		try {
			os = new DataOutputStream(s.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ch = new Checker();
		ch.start();
		
	}

	public void run(){
		//PrintWriter out = new PrintWriter(s.getOutputStream(), true);
		int i = 0;
		BufferedReader in;
		try {
			in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String inputLine, outputLine;
			while(true){
				try {
					if((inputLine = in.readLine()) != null){
						System.out.println("Fikk en linje");
						check(inputLine);
					}
				} catch (IOException e) {
					System.err.println("Feil 5");
					e.printStackTrace();
					in = new BufferedReader(new InputStreamReader(s.getInputStream()));
					i++;
					if(i == 10){
						this.stop();
					}
					
				}
			}
		} catch (IOException e1) {
			System.err.println("Feil 4");
			e1.printStackTrace();
		}
	}

	private void check(String inputLine) {
		//TO;+;FROM;+;MSG
		System.out.println(inputLine);
		String[] arr = inputLine.split(";;;");
		System.out.println("arr[0] == " + arr[0]);

		if(arr[0].compareToIgnoreCase("getfriends") == 0){
			String friends = cm.getFriends();
			
			try {
				os.writeBytes(friends + "\n");
			} catch (IOException e) {
				System.err.println("Feil 3");
				e.printStackTrace();
			}
		}
		else if(arr[0].compareToIgnoreCase("noOne") == 0){
			me = arr[1];
			System.out.println("Navnet mitt er " + me);
			cm.newClient(arr[1],s);
		}else if(arr[0].compareToIgnoreCase("exit") == 0){
			exitMethod();
		}else{
			if(arr.length > 2){
			cm.newMsg(arr[0], arr[1], arr[2]);
			}
		}
	
	}
	
	private void exitMethod() {
		ch = null;
		cm.delete(s);
		
	}

	private class Checker extends Thread{
		public void run(){
			while (true){
				try {
					sleep(4000);
					HashMap<String, Message> messages = cm.getHashMap();
					if(messages.size() > 0){
		
						for(Message m : messages.values()){
							if(!m.isSent){
								System.out.println("Melding til" + m.to + " Fra " + m.from);
							}
							if(!m.isSent && me.equalsIgnoreCase(m.to)){
								String send = "" + m.from + ";;;" +m.msg + "\n";
								m.isSent = true;
								os.writeBytes(send);
								os.flush();
								System.out.println("Sendte en melding automatisk \n" + send);
							}
						}
					}
				} catch (InterruptedException e) {
					System.err.println("Feil 1");
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					System.err.println("Feil 2");
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
}
